import { useBoardStore } from "@/store/modules/drawBoard";
import { getBoardInfo } from "@/config/basic-info-config";
const userId = `tiw_web_${Math.ceil(Math.random() * 1000)}`;

const awaitWrap = (promise) => {
  return promise.then((data) => [null, data]).catch((error) => [error, null]);
};

async function getUserSig() {
  const instance = window.axios.create({
    baseURL: "/board",
    timeout: 5000,
  });
  const res = await instance.get(
    `/get_tiw_token?userId=${userId}&timestamp=${Date.now()}`
  );
  return res.data;
}

async function joinClassroom(userSig) {
  const params = {
    sdkAppId: "1600048908",
    userId,
    classId: "123123",
    imGroupId: "", // IM群组ID（选填）
    userSig: String(userSig),
    nickname: "", // 昵称（选填）
    dowmGradeTimes: 0, // H5降级渲染参数（选填）
  };
  const boardStore = useBoardStore();
  boardStore.setClassInfo(params);
  return {
    isReady: true,
  };
}

export function joinHandler() {
  // 1. 获取签名信息
  /*const [error, res] = await awaitWrap(getUserSig());
  if (error) {
    console.error("获取签名出错, 请重试");
    return;
  }*/

  // const res = JSON.parse(sessionStorage.getItem("tuiRoom-userInfo"));
  const res = getBoardInfo();
  console.log(res, "哈哈哈");
  const params = {
    sdkAppId: res.sdkAppId,
    userId: res.userId,
    classId: "888",
    imGroupId: "", // IM群组ID（选填）
    userSig: res.userSig,
    nickname: "", // 昵称（选填）
    dowmGradeTimes: 0, // H5降级渲染参数（选填）
  };
  const boardStore = useBoardStore();
  boardStore.setClassInfo(params);
}
